package com.hnust.service.impl;

import com.hnust.dao.ClassDao;
import com.hnust.dao.CollegeDao;
import com.hnust.dao.MajorDao;
import com.hnust.dao.StudentDao;
import com.hnust.domain.Class;
import com.hnust.domain.Student;
import com.hnust.domain.record.AddOneStudent;
import com.hnust.domain.record.SearchStudentInfo;
import com.hnust.domain.record.SimpleStudentInfo;
import com.hnust.domain.record.StudentInfo;
import com.hnust.service.StudentService;
import com.hnust.util.IDUtil;
import com.hnust.util.PageVo;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("studentService")
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private ClassDao classDao;

    @Autowired
    private MajorDao majorDao;

    @Autowired
    private CollegeDao collegeDao;

    /**
     * 根据学院ID获取年级
     * @param collegeId 学院ID
     * @return
     */
    @Override
    public List<String> getAllGrade(String collegeId) {
        return classDao.findAllGrade(collegeId);
    }

    /**
     * 根据学院，年级获取所有专业
     * @param grade 年级
     * @param collegeId 学院ID
     * @return
     */
    @Override
    public List<String> getAllMajor( String grade,String collegeId) {
        List<String> majorIds=classDao.findAllMajorIdByGrade(grade,collegeId);
        List<String> majorNames=new ArrayList<>();
        for(String majorId:majorIds){
            majorNames.add(majorDao.findNameByMajorId(majorId));
        }
        return majorNames;
    }

    /**
     * 根据年级专业，学院，获取所有班号
     * @param grade 年级
     * @param major 专业
     * @param collegeId 学院ID
     * @return
     */
    @Override
    public List<String> getAllClass(String grade, String major, String collegeId) {
        String majorId=null;
        if(!major.equals("")){
            majorId=majorDao.findMajorIdByName(major);
        }
        return classDao.findAllClaNumberByMajorId(majorId,grade,collegeId);
    }

    @Override
    public List<Map<String, String>> getAllClassNew(String grade, String major, String collegeId) {
        String majorId=null;
        if(!major.equals("")){
            majorId=majorDao.findMajorIdByName(major);
        }
        return classDao.findAllClaNumberByMajorIdNew(majorId,grade,collegeId);
    }

    /**
     * 分页条件查询学生所有信息
     * @param collegeId 学院ID
     * @param searchStudentInfo 学生搜索条件类
     * @param rows 一行多少条
     * @param currentPage 当前页数
     * @return
     */
    @Override
	public PageVo<List<SimpleStudentInfo>> querySimpleStudenInfo(String collegeId, SearchStudentInfo searchStudentInfo,
																 Integer rows, Integer currentPage){

       if(searchStudentInfo!=null){
           //专业名字转专业Id
           searchStudentInfo.setMajor(majorDao.findMajorIdByName(searchStudentInfo.getMajor()));
       }
        //分页查询参数处理
        Integer limitM=(currentPage-1)*rows;
        Integer limitN=rows;
        //System.out.println(limitM+"\n"+limitN);

        //分页查询
        List<Student> students=studentDao.findStudentBySearch(collegeId,searchStudentInfo,limitN,limitM);
        if(students!=null){
            //数据封装
            List<SimpleStudentInfo> simpleStudentInfos =new ArrayList<>();
            for(Student student:students){
                SimpleStudentInfo simpleStudentInfo =new SimpleStudentInfo(
                        student.getStuId(),student.getAccount(),
                        student.getPassword(),student.getSchool(),
                        student.getName(),student.getStuNumber()
                );
                simpleStudentInfos.add(simpleStudentInfo);
            }
            Integer studentCount=studentDao.findStudentCountBySearch(collegeId,searchStudentInfo);
            //数据再次进行封装
            return new PageVo<List<SimpleStudentInfo>>(studentCount, simpleStudentInfos);
        }else{
            return null;
        }
    }

    /**
     * 删除学生信息
     * @param studentId 学生ID
     * @return
     */
    @Override
    public Boolean deleteStuRecord(String studentId) {
        return studentDao.deleteStudentById(studentId);
    }

    /**
     * 批量删除学生信息
     * @param studentIDs 学生ID们
     * @return
     */
    @Override
    public Boolean massDeleteStuRecord(List<String> studentIDs) {
        Boolean flag=true;
        for(String stuId:studentIDs){
            if(!studentDao.deleteStudentById(stuId)){
                flag=false;
            }
        }
        return flag;
    }


    /**
     * 重置学生密码
     * @param id 学生ID
     * @return
     */
    @Override
    public Boolean resetStuPwd(String id) {
        Student student=studentDao.getStudentById(id);
        return studentDao.updataStudentPassword(student.getStuNumber(), id);
    }


    /**
     * 获取某个学生的具体信息
     * @param stuId 学生ID
     * @return
     */
    @Override
    public StudentInfo getStudentInfo(String stuId) {

        //查询数据
        Student student=studentDao.getStudentById(stuId);
        if(student!=null){
            Class classN=classDao.getClassById(student.getClsId());
            //数据封装
            SimpleStudentInfo simpleStudentInfo =new SimpleStudentInfo(
                    student.getStuId(),student.getAccount(),
                    student.getPassword(),student.getSchool(),
                    student.getName(),student.getStuNumber()
            );
            //数据转换
            String majorName=majorDao.findNameByMajorId(classN.getMajId());
            String className=classN.getGrade()+"级"+majorName+classN.getClaNumber()+"班";
            String collegeName=collegeDao.findNameByCollegeId(classN.getColId());
            //再次数据封装
            return new StudentInfo(
                    simpleStudentInfo,student.getAlias(),
                    className,collegeName,student.getPhoNumber(),
                    student.getEMail()
            );
        }else {
            return null;
        }

    }

    /*批量导入学生*/
    @Override
    public Boolean addStudents(List<Student> students) throws Exception {
        int i=0;
        try {
            for (Student student : students) {
                i++;
                studentDao.insertStudent(student.getStuId(), student.getAccount(), student.getAlias(), student.getName(), student.getStuNumber(), student.getPhoNumber(), student.getEMail(), student.getPassword(), student.getSchool(), student.getClsId());
            }
        }catch (Exception e) {
            if ("class org.springframework.dao.DuplicateKeyException".equals(e.getClass().toString()))
                throw new Exception("数据已存在:问题发生在第" + i + "组数据");
            else{
                throw e;
            }
        }
        return true;
    }

    /**
     * 插入单个学生
     * @param studentInfo 信息
     * @return
     */
    @Override
    public Boolean addOneStudent(AddOneStudent studentInfo) {
        return studentDao.insertStudent(IDUtil.getPrimaryKey(), studentInfo.getAccount(), studentInfo.getAlias(), studentInfo.getName(), studentInfo.getStuNumber(), studentInfo.getPhone(), studentInfo.getEmail(), studentInfo.getPassword(), studentInfo.getSchool(), studentInfo.getClassName().toString()) >= 1;
    }


}
